package com.itheima.chapter01.respository;

import com.itheima.chapter01.Bean.Discuss;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//此接口用于JPA整合测试

public interface DiscussRepository extends JpaRepository<Discuss,Integer> {
//    1.查询author非空的Discuss评论集合
    public List<Discuss> findByAuthorNotNull();
//    2.根据文章id分页查询Discuss评论集合
    @Query("select c from t_comment c where c.aId = ?1")
    public List<Discuss> getDiscussPaged(Integer aid, Pageable pageable);
//    3.使用元素SQL语句，根据文章id分页查询Discuss评论集合
    @Query(value = "select * from t_comment where a_Id = ?1",nativeQuery = true)
    public List<Discuss> getDiscussPage2(Integer aid,Pageable pageable);
//    4.根据评论id修改评论作者author
    @Transactional
    @Modifying
    @Query("update t_comment c set c.author = ?1 where c.id = ?2")
    public int updateDiscuss(String author,Integer id);
//    5.根据评论id删除评论
    @Transactional
    @Modifying
    @Query("delete t_comment c where c.id = ?1")
    public int deleteDiscuss(Integer id);
}
